/*  -------------------------- (C) COPYRIGHT 2022 Fortiortech ShenZhen ---------------------------*/
/**
 * @copyright (C) COPYRIGHT 2022 Fortiortech Shenzhen
 * @file      main.c
 * @author    Fortiortech  Appliction Team
 * @since     Create:2022-07-14
 * @date      Last modify:2022-07-14
 * @note      Last modify author is Leo.li
 * @brief     包含主函数，软硬件初始化函数，VRER配置函数
 */

#include <MyProject.h>
/* Private variables ----------------------------------------------------------------------------*/
MotStaType mcState;
MotStaM    McStaSet;

/**
 * @brief        主函数，大循环运行偏置电流采集函数，电机状态机控制函数，以及环路响应函数
 * @date         2022-07-14
 */
void MC_Control(void)
{
    if (mcFaultSource != FaultNoSource)
    {
        mcState = mcFault;
    }
    
    switch (mcState)
    {
        case mcReady:
            Motor_Ready();
            
            if ((mcCurOffset.OffsetFlag == 1) && (MotorSpeed.FlagONOFF == 1))
            {
                mcState = mcInit;
                mcCurOffset.OffsetFlag = 0;
                mcCurOffset.OffsetCount = 0;    //偏置电压采集计数
                /**********电机目标方向*************/
                #if(FREnable ==0)
                {
                    MotorSpeed.TargetFR      = FRMODE;
                }
                #else
                {
                    if (FRPin == 0)                                             //  1:CCW ;0:CW
                    {
                        MotorSpeed.TargetFR  = CW;
                    }
                    else
                    {
                        MotorSpeed.TargetFR  = CCW;
                    }
                }
                #endif
            }
            
            break;
            
        case mcInit:
            if (mcCurOffset.OffsetFlag == 1)
            {
                Motor_Init();
                #if (IPMState == IPMtest)                           // 正常按电机状态机运行
                {
                    mcState                 = mcCharge;
                    mcFocCtrl.State_Count   = Charge_Time;
                }
                #else
                {
                    mcState                 = mcTailWind;         //6832可以不需要预充电，软件仅用来测试硬件
                    mcFocCtrl.State_Count   = 0;
                }
                #endif
            }
            
            break;
            
        case mcCharge:
            Motor_Charge();
            break;
            
        case mcTailWind:
            #if (TailWind_Mode == NoTailWind)
            {
                mcState                   = mcPosiCheck;
                mcFocCtrl.State_Count     = 0;
            }
            
            #else
            {
                Motor_TailWind();
				
				if(mcFocCtrl.State_Count == 0)
				{
					#if (TailWind_Mode ==RSDMethod)
					{
						RSDDealwith();
					}
					#elif (TailWind_Mode ==BEMFMethod)
					{
						BEMFDealwith();
					}
					#elif (TailWind_Mode ==FOCMethod)
					{
						FOCTailWindDealwith();
					}
					#endif
				
				}
            }
            #endif
            break;
            
        case mcPosiCheck:
            mcFocCtrl.mcPosCheckAngle = 0;
            mcState = mcAlign;
            mcFocCtrl.State_Count = Align_Time;
            break;
            
        case mcAlign:
            Motor_Align();
            #if (AlignTestMode == 1)
            {
                while (1);
            }
            #else
            {
                if (mcFocCtrl.State_Count == 0)
                {
                    mcState = mcStart;
                }
            }
            #endif
            break;
            
        case mcStart:
            Motor_Open();
            mcState   = mcRun;
            break;
            
        case mcRun:
            if (MotorSpeed.FlagONOFF == 0)
            {
                mcState = mcStop;
                mcFocCtrl.State_Count = 600;
            }
            
            break;
            
        case mcStop:
            Motor_Stop();
            break;
            
        case mcBrake:
            if (mcFocCtrl.State_Count == 0)
            {
                mcState = mcReady;
                ClrBit(DRV_CR, DRVEN);
                MOE = 0;
                ClrBit(DRV_CR, FOCEN);
            }
            
            break;
            
        case mcFault:
            if (mcFaultSource == FaultNoSource)   //保护清零后关闭输出，软件重新运行
            {
                mcState   = mcReady;
            }
            else
            {
                FaultProcess();
            }
            
            break;
            
        default:
            break;
    }
}

